home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3713 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.2 KB

  1. Path: nntp-xfer-2.csn.net!yuma!steffend
  2. From: steffend@lamar.colostate.edu (Dave Steffen)
  3. Newsgroups: comp.lang.c++,comp.lang.c
  4. Subject: Re: Performance: C vs. C++
  5. Followup-To: comp.lang.c++,comp.lang.c
  6. Date: 25 Jan 1996 18:55:19 GMT
  7. Organization: Colorado State University, Fort Collins, CO  80523
  8. Message-ID: <4e8jmn$2oo4@yuma.ACNS.ColoState.EDU>
  9. References: <30F6BAAC.12B5@iastate.edu> <31002F05.1B53@qm.claris.com>
  10. NNTP-Posting-Host: glitch.physics.colostate.edu
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Steve Lee wrote: 
  14.  
  15. >I was wondering if anyone could tell me what the difference in
  16. > performance would be between writing a program in C++ vs. C.  Assume
  17. > that the compilers are both of equal quality.  I know that certain
  18. > features such as virtual functions and exception handling add
  19. > overhead.  Are there any others?  If you don't implement these
  20. > features that add the extra overhead, is C++ directly comparable to
  21. > C in performance then?  The only other reason I could see C++ being
  22. > slower than C is programming style.  Object-oriented is slower since
  23. > there are more layers, hence function call overhead.
  24.  
  25.  
  26. Raymond Fischer responded in part:
  27.  
  28.  
  29. > Hmmm.  Two answers to that one.  Both somewhat subjective.
  30.  
  31. > First: A program that doesn't run doesn't run quickly.  C++ can make
  32. > for fewer bugs in the resulting program and can allow you to produce
  33. > a working program quicker.  In that sense C++ is faster.
  34.  
  35.     A very good point; my heavy numerical code is C++, with C++
  36. classes wrapping LAPACK calls. The improvement in readability has to
  37. be seen to be believed.
  38.  
  39. > Second: C++ tends to be slower in execution - assuming that you use
  40. > C++ features.  Virtual functions involve lookups, although you can
  41. > avoid that by not using virtual functions.  The latest compilers (on
  42. > the Mac) provide zero-overhead exceptions.  But where C++ tends to
  43. > cause the greatest problems with performance is in hidden function
  44. > calls.  The most notorious of these is the call of the copy
  45. > constructor in parameters.  Other sources are various type
  46. > conversions.  C++ is slower not because it's an inherently slower
  47. > language, but because people tend to use it differently.  With due
  48. > care there should be no significant performance hit.
  49.  
  50.     Exactly. With some care in writing my classes (and some minor
  51. tweaking) I'm getting performance out of G++ 2.7.0 that's within 5% of
  52. the original Fortran code - as compiled by a native
  53. compiler. Sometimes it's even faster.
  54.  
  55.     One of my advisor's colleagues has done some kind of _really_
  56. fancy LAPACK wrapper in C++. He says he's getting through-put that's
  57. significantly better than anything he's ever seen Fortran do. I'm
  58. still trying to figure out how he did it... ;-)
  59.  
  60.     Moral of the story: C++ can _FLY_... provided you know what
  61. you're doing. ;-)
  62.  
  63.                                  /\
  64.                                  \/
  65.  
  66. Dave Steffen                      No, his mind is not for rent
  67. Dept. of Physics                  To any God or Government
  68. Colorado State University         Always hopeful, yet discontent
  69. steffend@lamar.colostate edu      He knows changes aren't permanent-
  70.                       But change is...
  71. "Speak softly...                    
  72. ... and carry a black belt!"              -Neal Peart / RUSH
  73. -----------------------------------------------------------------------
  74.